package com.dormitoryAdministratorSystem.ui.roommanage;

import com.dormitoryAdministratorSystem.entity.maintain.Maintain;
import com.dormitoryAdministratorSystem.entity.room.RoomBean;
import com.dormitoryAdministratorSystem.service.maintain.impl.MaintainSerImpl;
import com.dormitoryAdministratorSystem.service.room.impl.RoomSerImpl;
import com.dormitoryAdministratorSystem.ui.MainFrame;
import com.dormitoryAdministratorSystem.util.BeansUtil;
import com.dormitoryAdministratorSystem.util.CheckFormatUtil;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoDate;
import com.lovo.netCRM.component.LovoTable;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;

public class RoomEqpDialog extends JDialog {

	private LovoTable recordTab;

	private LovoDate reportDate;// 报损日期
	private JLabel descriptLab;
	private JTextArea descriptArea;

	private LovoButton addRecordBtn;
	private LovoButton processBtn;
	private LovoButton exitBtn;

	private int id;
	private MaintainSerImpl msi = new MaintainSerImpl();
	private RoomSerImpl rsi = new RoomSerImpl();

	public RoomEqpDialog(MainFrame frame, int id) {
		// TODO Auto-generated constructor stub
		super(frame, "房间设施维护", true);
		this.id = id;
		this.setBounds(350, 250, 420, 400);
		this.setLayout(null);
		this.initTable();
		this.init();
		this.initBtn();

		this.setVisible(true);
	}

	private void initTable() {
		this.recordTab = new LovoTable(this, new String[] { "损坏描述", "报损日期",
				"是否解决" }, new String[] {"damage", "reportTime", "isSolve"},// 数组中应该是对应属性的属性名
				"mId");// 填入唯一标示属性
		this.recordTab.setSizeAndLocation(20, 10, 360, 150);
		this.recordTab.updateLovoTable(msi.getMaintainInfos(this.id));
	}

	private void init() {
		// TODO Auto-generated method stub
		this.reportDate = new LovoDate("报损日期", 80, 180, this);

		this.descriptLab = new JLabel("损坏描述");
		this.descriptLab.setBounds(80, 220, 60, 30);
		this.add(this.descriptLab);

		this.descriptArea = new JTextArea();
		JScrollPane jsp = new JScrollPane(this.descriptArea);
		jsp.setBounds(150, 220, 220, 100);
		this.add(jsp);
	}

	private void initBtn() {
		// TODO Auto-generated method stub
		this.addRecordBtn = new LovoButton("添加设施损坏记录", 10, 330, this);
		this.addRecordBtn.setSize(120, 20);

		this.processBtn = new LovoButton("处理已损坏设备", 145, 330, this);
		this.processBtn.setSize(120, 20);

		this.exitBtn = new LovoButton("退出", 280, 330, this);
		this.exitBtn.setSize(120, 20);

		this.addRecordBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(descriptArea == null || "".equals(descriptArea.getText())){
					JOptionPane.showMessageDialog(null, "描述不能为空");
				}else if(reportDate == null || "".equals(reportDate.getText())){
					JOptionPane.showMessageDialog(null, "日期不能为空");
				}else if(!CheckFormatUtil.checkLength(descriptArea.getText().trim(), 2, 20)){
					JOptionPane.showMessageDialog(null,
							"添加失败，信息描述范围：2-20");
				}else{
					try {
						Maintain m = new Maintain();
						m.setDamage(descriptArea.getText().trim());
						m.setReportTime(new Date(reportDate.getDate().getTime()));
						m.setRoomBean(BeansUtil.getRoom(RoomEqpDialog.this.id));
						m.setIsSolve("否");
						if (msi.addMaintaininfo(m) == 1) {
							recordTab.updateLovoTable(msi.getMaintainInfos(RoomEqpDialog.this.id));
							RoomBean r = rsi.getRoomInfo(RoomEqpDialog.this.id);
							JOptionPane.showMessageDialog(null, "上报成功");
							RoomEqpDialog.this.descriptArea.setText("");
							RoomEqpDialog.this.reportDate.setText("");
							if("正常".equals(r.getRoomState())){
								rsi.changeRoomState("已损坏", RoomEqpDialog.this.id);
							}

							//RoomEqpDialog.this.dispose();
						} else {
							JOptionPane.showMessageDialog(null, "上报失败");
						}
					}catch (Exception e){
						JOptionPane.showMessageDialog(null, "上报失败，日期错误或其他原因");
					}
				}
			}
		});
		this.processBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int id = recordTab.getKeyByInt();
				if(id == -1 ){
					JOptionPane.showMessageDialog(null, "请先选择一个设施");
				}else{
					Maintain m = msi.showMainInfo(id);
					if("是".equals(m.getIsSolve())){
						JOptionPane.showMessageDialog(null, "已经处理了");
					}else{
						m.setIsSolve("是");
						if(msi.updateInfo(m) == 1){
							JOptionPane.showMessageDialog(null, "成功");
							recordTab.updateLovoTable(msi.getMaintainInfos(RoomEqpDialog.this.id));
						}else{
							JOptionPane.showMessageDialog(null, "失败");
						}
//						RoomEqpDialog.this.dispose();
					}
				}
			}
		});
		this.exitBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub

				RoomEqpDialog.this.dispose();
			}
		});
	}

}
